
package horde.minefield.andres.netCheck;

import horde.interfaces.Requests;
import horde.patterns.Core;
import horde.patterns.Msg;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * @author Andres Picazo
 */
public class Bot extends Core {

    public void test () {
        Console c = new Console (this.requests, this.getID());
        try {
            c.run();
        } catch (Exception e) {

        }
//        Scanner s = new Scanner(System.in);
//        print ("Write exit to close");
//        String op, command; // = s.nextLine();
//        do {
//            print ("send <text> : sends a message to every agent in the default group\n");
//            op = s.nextLine();
//            StringTokenizer st = new StringTokenizer(op);
//            command = st.nextToken();
//            print ("Read: " + command);
//            if (command.equals("send")) {
//                String m = this.getID() + ":";
//                while (st.hasMoreTokens()) m += " " + st.nextToken();
//                print ("sending: " + m);
//                this.requests.post(Msg.composeForGroup(this.getID(), "starwars", "receive", m));
//            }
//        } while (!command.equals("exit"));
//        print ("Finished");
//        Directory.getInstance().getRequests().halt();
    }

    private void print (String t) {
        System.err.println (t);
    }

    public void receive (String message) {
        print ("Received message: " + message);
    }

    private class Console implements Runnable {

        private Requests requests;
        private String id;

        public Console (Requests r, String ID) {
            requests = r;
            id = ID;
        }

        public void run() {
            Scanner s = new Scanner(System.in);
            print ("Write exit to close");
            String op, command; // = s.nextLine();
            do {
                print ("send <text> : sends a message to every agent in the default group\n");
                op = s.nextLine();
                StringTokenizer st = new StringTokenizer(op);
                command = st.nextToken();
                print ("Read: " + command);
                if (command.equals("send")) {
                    String m = id + ":";
                    while (st.hasMoreTokens()) m += " " + st.nextToken();
                    print ("sending: " + m);
                    this.requests.post(Msg.composeForGroup(id, "starwars", "receive", m));
                }
            } while (!command.equals("exit"));
            print ("Finished");
        }
    }
}
